#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 crane <crane@sharp>
#
# Distributed under terms of the MIT license.

"""

"""

class Solution:
    """
    @param s: a string
    @return: an integer
    """
    def lengthOfLongestSubstring(self, s):
        cur_len, max_len = 0, 0
        self.idx_hash = {}  # {char : occur_idx}
        self.s = s

        left = 0
        # right = 0

        for idx, c in enumerate(s):
            find_idx = self.idx_hash.get(c)
            if find_idx is None:
                # print('not', idx, c)
                cur_len += 1
                max_len = max(max_len, cur_len)
            else:
                # 不可能是最大, max_len不变
                # print('find', idx, c)
                cur_len = idx - find_idx
                # 先删除, 再修改left的值
                self.remove_hash(left, find_idx)
                left = find_idx + 1

            # right += 1
            self.idx_hash[c] = idx
            # print(left, self.idx_hash, cur_len, max_len)
            # print()

        return max_len

    def remove_hash(self, left, right):
        # remove [left, right]
        # print(left, right)
        for idx in range(left, right+1):
            c = self.s[idx]
            del self.idx_hash[c]


def test():
    s = Solution()
    # ret = s.lengthOfLongestSubstring("abcdbez")
    ret = s.lengthOfLongestSubstring("an++--viaj")
    print(ret)

def main():
    print("start main")
    test()

if __name__ == "__main__":
    main()
